<?php

namespace app\Server;

use app\model\CurrentStock;
use app\model\DispatchLists;
use app\model\T_PM_MO_BASE;
use app\model\T_PM_PROJECT_BASE;
use app\model\T_PM_PROJECT_DETAIL;
use app\model\T_WMS_PO_INFO;
use app\model\T_WMS_SPOT_ITEM_INFO;
use app\model\T_WMS_STOCK_DOC;
use app\model\T_WMS_STOCK_INFO;
use app\model\T_WMS_STOCK_ITEM;

class Operatingcockpit
{
    public $CurrentStock;
    public $T_WMS_STOCK_INFO;
    public $T_WMS_SPOT_ITEM_INFO;
    public $T_PM_PROJECT_BASE;
    public $T_PM_MO_BASE;
    public $T_PM_PROJECT_DETAIL;
//    public $T_WMS_STOCK_ITEM;
    public $T_WMS_PO_INFO;
//    public $T_WMS_STOCK_DOC;
    public $DispatchLists;
    public function __construct()
    {
        $this->CurrentStock=(new CurrentStock());
        $this->T_WMS_STOCK_INFO=(new T_WMS_STOCK_INFO());
        $this->T_WMS_SPOT_ITEM_INFO=(new T_WMS_SPOT_ITEM_INFO());
        $this->T_PM_PROJECT_BASE=(new T_PM_PROJECT_BASE());
        $this->T_PM_MO_BASE=(new T_PM_MO_BASE());
        $this->T_PM_PROJECT_DETAIL=(new T_PM_PROJECT_DETAIL());
//        $this->T_WMS_STOCK_ITEM=(new T_WMS_STOCK_ITEM());
        $this->T_WMS_PO_INFO=(new T_WMS_PO_INFO());
//        $this->T_WMS_STOCK_DOC=(new T_WMS_STOCK_DOC());
        $this->DispatchLists=(new DispatchLists());
    }

    public function index(){
        //生产部管理指标数据计算
        $data2=$this->produce();
        //呆滞料
        $data3=$this->T_WMS_STOCK_INFO->index([['CREATE_TIME','<=',date('Y-m-d',strtotime('-180 day'))],['OUT_DOC','=','']]);
        foreach ($data3 as $k=>$v){
            $data3[$k]['INVALID_DATE']=substr($v['INVALID_DATE'],0,16);
            $data3[$k]['CREATE_TIME']=substr($v['CREATE_TIME'],0,16);
            $data3[$k]['ITEM_QTY']=intval($v['ITEM_QTY']);
        }
        $data3['data']=$data3;
        $data3['count']=count($data3['data']);

        //U8-MES库存差异
        $cy['data'] = $this->CurrentStock->index($this->T_WMS_STOCK_INFO,$this->T_WMS_SPOT_ITEM_INFO);
//        $cy=$cy->toarray();
//        $newCy=[];
//        foreach ($cy['data'] as $k=>$v){
//            if($v['cy_number']!=0){
//                $newCy[]=$v;
//            }
//        }
//        $newCy['data']=$newCy;
//        $newCy['count']=count($newCy['data']);
        $cy['count']=count($cy['data']);
        //生产工单
        $list=$this->T_PM_PROJECT_BASE->index();
        $MODEL_CODES=array_column($list,'MODEL_CODE');
        $cSoCodes=$this->DispatchLists->whereIn('cInvCode',$MODEL_CODES)->field('cSoCode')->select()->toArray();
        foreach($MODEL_CODES as $k=>$v){
            $list[$k]['cSoCode']=$cSoCodes[$k]['cSoCode'];
        }
        foreach($list as $k=>$v){
            //判断工单是否超期,根据状态判断，正常关结，
            if($v['STATUS']==3 || $v['STATUS']==4){
                if($v['PLAN_END_TIME']!='' && $v['ACTUAL_END_TIME']!=''){
                    if($v['PLAN_END_TIME']>$v['ACTUAL_END_TIME']){
                        $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                    }else{
                        $list[$k]['cq']="<span style='color: white;background-color: red'>是</span>";
                    }
                }elseif ($v['PLAN_END_TIME']!= ''&& $v['ACTUAL_END_TIME']==''){
                    if($v['PLAN_END_TIME']<date('Y-m-d')){
                        $list[$k]['cq']="<span style='color: white;background-color: red'>是</span>";
                    }else{
                        $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                    }
                }else{
                    $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                }
            }elseif ($v['STATUS']==1 || $v['STATUS']==2){
                if($v['PLAN_END_TIME']<date('Y-m-d')){
                    $list[$k]['cq']="<span style='color: white;background-color: red'>是</span>";
                }else{
                    $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                }
            }
            //判断状态
            switch($v['STATUS']){
                case 1:$list[$k]['STATUS']='开立';break;
                case 2:$list[$k]['STATUS']='投产';break;
                case 3:$list[$k]['STATUS']='关结';break;
                case 4:$list[$k]['STATUS']='强制关结';break;
            }
            //计算每个工单对应制令单的不良率
            $counts=$this->T_PM_MO_BASE->index($v['ID'],'INPUT_QTY');
            $counts2=$this->T_PM_MO_BASE->index($v['ID'],'OUTPUT_QTY');
            if($counts2!=0){
                $str=(1-$counts2/$counts);
                $list[$k]['NgCount']=$str<0?0:substr((string)$str,0,6);
            }else{
                $list[$k]['NgCount']=0;
            }
            //日期截取
            $list[$k]['CREATE_TIME']=substr($v['CREATE_TIME'],0,16);
            if($v['CLOSE_TIME']!='')$list[$k]['CLOSE_TIME']=substr($v['CLOSE_TIME'],0,16);
            if($v['ACTUAL_END_TIME']!='')$list[$k]['ACTUAL_END_TIME']=substr($v['ACTUAL_END_TIME'],0,16);

        }
        $list['data']=$list;
        $list['count']=count($list['data']);
        return ['newCy'=>$cy,'list'=>$list,'data2'=>$data2,'data3'=>$data3];
    }
    /**
     * 生产部管理指标
     */
    public function produce($day=90){
        //工单不良率1-产出/投入
        $sc_gd_bl_max=$this->T_PM_PROJECT_BASE->sc_gd_bl_avg($day,'desc');
        $sc_gd_bl_min=$this->T_PM_PROJECT_BASE->sc_gd_bl_avg($day);
        $sc_gd_bl_avg=($sc_gd_bl_max['NG']+$sc_gd_bl_min['NG'])/2;
        $list=$this->T_PM_PROJECT_BASE->produce($day);
        $list['count']=count($list);
        $cqCount=0;
        $Ngcounts=0;
        foreach ($list as $v){
            //判断工单是否超期，计划完工时间<实际完工时间
            if($v['PLAN_END_TIME']==null){
                //判断创建时间是否<实际完工时间
                if($v['CREATE_TIME']<$v['ACTUAL_END_TIME']){
                    $cqCount++;
                }
            }else{
                if ($v['PLAN_END_TIME'] > $v['ACTUAL_END_TIME']) {
                    $cqCount++;
                }
            }
            //计算每个工单对应制令单的不良率
            $counts=$this->T_PM_MO_BASE->index($v['ID'],'INPUT_QTY');
            $counts2=$this->T_PM_MO_BASE->index($v['ID'],'OUTPUT_QTY');
            if($counts2!=0){
//                $str=1-($counts2)/($counts);
//                if($str>0.0005){
//                    $Ngcounts++;
//                }
                (1-($counts2)/($counts))>0.0005?$Ngcounts++:'';
            }
            $PROJECT_NOS[]=$v['PROJECT_NO'];
        }
        //订单准备周期：最大，最小，平均
        $sc_gd_zb_time_max=$this->T_PM_PROJECT_DETAIL->produce($PROJECT_NOS,'desc');
        $sc_gd_zb_time_min=$this->T_PM_PROJECT_DETAIL->produce($PROJECT_NOS,'asc');
        if(empty($sc_gd_zb_time_min['time']))$sc_gd_zb_time_min['time']=0;
        $sc_gd_zb_time_avg=($sc_gd_zb_time_min['time']+$sc_gd_zb_time_max['time'])/2;

        //计算生产部管理指标
        //默认90天内
        //工单完结率,关节工单数/总工单数
        $sc_gd_gj_count=$this->T_PM_PROJECT_BASE->sc_gd_gj_count($day,[3,4]);
        $sc_gd_z_count=$this->T_PM_PROJECT_BASE->sc_gd_gj_count($day,[1,2,3,4]);
        $sc_gd_gjl=substr((string)($sc_gd_gj_count/$sc_gd_z_count*100),0,5);
        //工单完结周期
        $sc_gd_wj_time_max=$this->T_PM_PROJECT_BASE->sc_gd_wj_zq($day,"desc");
        $sc_gd_wj_time_min=$this->T_PM_PROJECT_BASE->sc_gd_wj_zq($day,"asc");
        $sc_gd_wj_time_avg=($sc_gd_wj_time_min['time']+$sc_gd_wj_time_max['time'])/2;
        //超均值工单比例
        $sc_gd_avg_count=$this->T_PM_PROJECT_BASE->sc_gd_cz_bl($day);
        $sc_gd_count=count($sc_gd_avg_count);
        $count=0;
        foreach ($sc_gd_avg_count as $k=>$v){
            $c=floor((strtotime($v['CLOSE_TIME'])-strtotime($v['CREATE_TIME']))/86400);
            if($c>$sc_gd_wj_time_avg){
                $count++;
            }
        }
        $sc_gd_cjz_bl=substr((string)($count/$sc_gd_count),0,5);
        //超期工单比例
        $cqCount=substr((string)($cqCount/$list['count']),0,5);
        

        //采购周期:最大，最小，平均
        $cg_zq_max=$this->T_WMS_PO_INFO->produce($day,'desc');
        $cg_zq_min=$this->T_WMS_PO_INFO->produce($day,'asc');
        $cg_zq_avg=($cg_zq_max['time']+$cg_zq_min['time'])/2;

        //上线前准备周期
        $zld_sx_max=$this->T_PM_MO_BASE->produce($day,'desc');
        $zld_sx_min=$this->T_PM_MO_BASE->produce($day,'asc');
        $zld_sx_avg=($zld_sx_max['time']+$zld_sx_min['time'])/2;

        //制造周期

        $zld_zz_max=$this->T_PM_MO_BASE->zld_zz_avg($day,'desc');
        $zld_zz_min=$this->T_PM_MO_BASE->zld_zz_avg($day,'asc');
        $zld_zz_avg=($zld_zz_max['time']+$zld_zz_min['time'])/2;
        return [
            'sc_gd_gjl'=>$sc_gd_gjl
            ,'sc_gd_wj_time_max'=>$sc_gd_wj_time_max['time']
            ,'sc_gd_wj_time_min'=>$sc_gd_wj_time_min['time']
            ,'sc_gd_wj_time_avg'=>$sc_gd_wj_time_avg
            ,'sc_gd_cjz_bl'=>$sc_gd_cjz_bl
            ,'cqCount'=>$cqCount
            ,'sc_gd_bl_max'=>'0'.substr((string)$sc_gd_bl_max['NG'],0,4)
            ,'sc_gd_bl_min'=>'0'.substr((string)$sc_gd_bl_min['NG'],0,4)
            ,'sc_gd_bl_avg'=>substr((string)$sc_gd_bl_avg,0,5)
            ,'Ngcounts'=>$Ngcounts
            ,'sc_gd_zb_time_max'=>$sc_gd_zb_time_max['time']
            ,'sc_gd_zb_time_min'=>$sc_gd_zb_time_min['time']
            ,'sc_gd_zb_time_avg'=>$sc_gd_zb_time_avg
            ,'cg_zq_max'=>$cg_zq_max['time']
            ,'cg_zq_min'=>$cg_zq_min['time']
            ,'cg_zq_avg'=>$cg_zq_avg
            ,'zld_sx_max'=>$zld_sx_max['time']
            ,'zld_sx_min'=>$zld_sx_min['time']
            ,'zld_sx_avg'=>$zld_sx_avg
            ,'zld_zz_max'=>$zld_zz_max['time']
            ,'zld_zz_min'=>$zld_zz_min['time']
            ,'zld_zz_avg'=>$zld_zz_avg
        ];
    }

}